package com.skillworld.webapp.model.department;

import java.util.Collection;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.MapKeyColumn;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

import com.skillworld.webapp.model.gamemap.GameMap;
import com.skillworld.webapp.model.item.ItemDetails;
import com.skillworld.webapp.model.user.User;


@Entity
@org.hibernate.annotations.BatchSize(size=10)
@Table(name="Department")
public class Department {

	private Long departmentId;
	private Map<Locale,DepartmentDetails> localizedDetails;
	private GameMap gameMap;
	private Set<User> leaders;
			
	
	protected Department()
	{
	    
	}

	public Department(GameMap gameMap) {
        super();
        this.gameMap = gameMap;
    }

    @SequenceGenerator(name = "DepartmentIdGenerator", sequenceName = "DepartmentSeq")
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "DepartmentIdGenerator")
	@Column(name = "idDepartment")
	public Long getDepartmentId() {
		return departmentId;
	}
	
	public void setDepartmentId(Long departmentId) {
		this.departmentId = departmentId;
	}
	
	@ElementCollection(fetch=FetchType.EAGER)
    @CollectionTable(name = "DepartmentDetails",
                     joinColumns = @JoinColumn(name = "idDepartment"))
    @MapKeyColumn(name = "locale")
	protected Map<Locale, DepartmentDetails> getLocalizedDetails() {
        return localizedDetails;
    }

    protected void setLocalizedDetails(Map<Locale, DepartmentDetails> localizedDetails) {
        this.localizedDetails = localizedDetails;
    }

    @OneToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "idMap")
	public GameMap getGameMap() {
		return gameMap;
	}
	
	public void setGameMap(GameMap gameMap) {
		this.gameMap = gameMap;
	}

    @ManyToMany(fetch=FetchType.LAZY)
    @JoinTable(name="DepartmentLeader",
               joinColumns=@JoinColumn(name="idDepartment"),
               inverseJoinColumns=@JoinColumn(name="idUser")
    )
    @Cascade(CascadeType.SAVE_UPDATE)
    public Set<User> getLeaders() {
        return leaders;
    }

    protected void setLeaders(Set<User> leaders) {
        this.leaders = leaders;
    }
    
    public void addLeader(User user)
    {
        this.getLeaders().add(user);
    }
    
    public void removeLeader(User user)
    {
        this.getLeaders().remove(user);
    }
    
    public void replaceLeaders(Collection<User> newLeaders)
    {
        this.getLeaders().clear();
        this.getLeaders().addAll(newLeaders);
    }
	
    @Transient
    public DepartmentDetails getDetails(Locale locale)
    {
        return this.getLocalizedDetails().get(locale);
    }
    
    @Transient
    public String getName(Locale locale)
    {
        return this.getDetails(locale).getName();
    }
    
    @Transient
    public String getInfo(Locale locale)
    {
        return this.getDetails(locale).getInfo();
    }
	
}
